package de.htwds.mada.todolistDB.db;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;

import de.htwds.mada.todolistDB.model.Category;
import de.htwds.mada.todolistDB.model.Priority;
import de.htwds.mada.todolistDB.model.Todo;
import de.htwds.mada.todolistDB.model.TodoCategory;

public class Repository {

	private Dao<Priority, Integer> priorityDao;
	private Dao<Category, Integer> categoryDao;
	private Dao<Todo, Integer> todoDao;
	private Dao<TodoCategory, Integer> todocategoryDao;

	public Repository(final DatabaseHelper databaseHelper) {
		this.priorityDao = getPriorityDao(databaseHelper);
		this.categoryDao = getCategoryDao(databaseHelper);
		this.todoDao = getTodoDao(databaseHelper);
		this.todocategoryDao = getTodoCategoryDao(databaseHelper);
	}

	public List<Priority> getPriorities() throws SQLException {
		return this.priorityDao.queryForAll();
	}

	public List<Category> getCategories() throws SQLException {
		return this.categoryDao.queryForAll();
	}

	public List<Todo> getTodos() throws SQLException {
		return this.todoDao.queryForAll();
	}

	public List<TodoCategory> getTodoCategories() throws SQLException {
		return this.todocategoryDao.queryForAll();
	}

	public List<TodoCategory> getTodoCategories(Todo todo) throws SQLException {
		return this.todocategoryDao.queryForEq("todo_id", todo.getId());
	}

	public void createOrUpdatePriority(Priority prio) throws SQLException {
		this.priorityDao.createOrUpdate(prio);
	}

	public void creatOrUpdateCategory(Category cat) throws SQLException {
		this.categoryDao.createOrUpdate(cat);
	}

	public void createOrUpdateTodo(Todo todo) throws SQLException {
		this.todoDao.createOrUpdate(todo);
	}

	public void createOrUpdateTodoCategory(TodoCategory tc) throws SQLException {
		this.todocategoryDao.createOrUpdate(tc);
	}

	public void deletePriority(Priority prio) throws SQLException {
		if (prio != null) {
			if (this.todoDao.queryForEq("priority_id", prio.getId()).size() != 0)
				throw new SQLException();
			else
				this.priorityDao.delete(prio);
		}
	}

	public void deleteCategory(Category cat) throws SQLException {
		if (cat != null) {
			if (this.todocategoryDao.queryForEq("category_id", cat.getId())
					.size() != 0)
				throw new SQLException();
			else
				this.categoryDao.delete(cat);
		}
	}

	public void deleteTodo(Todo todo) throws SQLException {
		if (todo != null)
			this.todoDao.delete(todo);
	}

	public void deleteTodoCategory(TodoCategory todocat) throws SQLException {
		if (todocat != null)
			this.todocategoryDao.delete(todocat);
	}

	private Dao<Priority, Integer> getPriorityDao(DatabaseHelper databaseHelper) {
		if (this.priorityDao == null) {
			try {
				this.priorityDao = databaseHelper.getPriorityDao();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.priorityDao;
	}

	private Dao<Category, Integer> getCategoryDao(DatabaseHelper databaseHelper) {
		if (this.categoryDao == null) {
			try {
				this.categoryDao = databaseHelper.getCategoryDao();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.categoryDao;
	}

	private Dao<Todo, Integer> getTodoDao(DatabaseHelper databaseHelper) {
		if (this.todoDao == null) {
			try {
				this.todoDao = databaseHelper.getTodoDao();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.todoDao;
	}

	private Dao<TodoCategory, Integer> getTodoCategoryDao(
			DatabaseHelper databaseHelper) {
		if (this.todocategoryDao == null) {
			try {
				this.todocategoryDao = databaseHelper.getTodocategoryDao();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.todocategoryDao;
	}

}
